package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Color;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.border.EtchedBorder;
import javax.swing.JTextField;

import cliente.Cliente;
import cliente.ClienteJaCadastradoException;
import cliente.CpfInvalidoException;
import fachada.Fachada;

import java.sql.SQLException;

public class GuiCliente extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtNome;
	private JTextField txtCpf;
	private JTextField txtEndereco;
	private JTextField txtTelefone;
	private JTextField txtEmail;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					GuiCliente frame = new GuiCliente();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GuiCliente() {
		setTitle("CLIENTE");
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 470, 439);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel.setBackground(Color.WHITE);
		panel.setBounds(10, 326, 444, 73);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton btnCadastrar = new JButton("Cadastrar");
		btnCadastrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					CadastrarCliente();
				} catch (ClienteJaCadastradoException e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage());
					e1.printStackTrace();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnCadastrar.setBounds(10, 11, 97, 49);
		panel.add(btnCadastrar);
		
		JButton btnNewButton = new JButton("Editar");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnNewButton.setBounds(117, 11, 97, 49);
		panel.add(btnNewButton);
		
		JButton btnRemover = new JButton("Remover");
		btnRemover.setBounds(224, 11, 97, 49);
		panel.add(btnRemover);
		
		JButton btnProcurar = new JButton("Procurar");
		btnProcurar.setBounds(331, 11, 97, 49);
		panel.add(btnProcurar);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		panel_1.setBackground(Color.WHITE);
		panel_1.setBounds(24, 11, 416, 175);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		JLabel lblNome = new JLabel("Nome");
		lblNome.setBounds(10, 11, 36, 20);
		panel_1.add(lblNome);
		
		txtNome = new JTextField();
		txtNome.setBounds(10, 31, 394, 20);
		panel_1.add(txtNome);
		txtNome.setColumns(10);
		
		JLabel lblCpf = new JLabel("CPF");
		lblCpf.setBounds(10, 62, 36, 20);
		panel_1.add(lblCpf);
		
		txtCpf = new JTextField();
		txtCpf.setBounds(10, 82, 101, 20);
		panel_1.add(txtCpf);
		txtCpf.setColumns(10);
		
		JLabel lblEndereo = new JLabel("Endere\u00E7o");
		lblEndereo.setBounds(10, 112, 54, 20);
		panel_1.add(lblEndereo);
		
		txtEndereco = new JTextField();
		txtEndereco.setBounds(10, 132, 394, 20);
		panel_1.add(txtEndereco);
		txtEndereco.setColumns(10);
		
		JLabel lblTelefone = new JLabel("Telefone");
		lblTelefone.setBounds(121, 62, 54, 20);
		panel_1.add(lblTelefone);
		
		txtTelefone = new JTextField();
		txtTelefone.setBounds(121, 82, 107, 20);
		panel_1.add(txtTelefone);
		txtTelefone.setColumns(10);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(238, 62, 36, 20);
		panel_1.add(lblEmail);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(238, 82, 166, 20);
		panel_1.add(txtEmail);
		txtEmail.setColumns(10);
	}
	
	private void CadastrarCliente() throws ClienteJaCadastradoException, CpfInvalidoException, SQLException, Exception{
		Cliente cliente = new Cliente();
		cliente.setNome(txtNome.getText());
		cliente.setCpf(txtCpf.getText());
		cliente.setEndereco(txtEndereco.getText());
		cliente.setTelefones(txtTelefone.getText());
		cliente.setEmails(txtEmail.getText());
		
		try{
			Fachada.getInstance().ClienteCadastrar(cliente);
			txtNome.setText(null);
			txtCpf.setText(null);
			txtEndereco.setText(null);
			txtEmail.setText(null);
			txtTelefone.setText(null);
		}
		catch(ClienteJaCadastradoException e){
			throw new ClienteJaCadastradoException(cliente.getCpf());
		}
		
		
		
		
		
		
	}
}
